#include <windows.h>											// Header File For Windows

#include "GL_Window.h"
#include "Loop.h"

/* Process Window Message Callbacks
 * darn thing can't be in a class, because we need to pass a pointer to his function */
LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// Get The Window Context
	GL_Window* window = (GL_Window*)(GetWindowLong (hWnd, GWL_USERDATA));

	switch (uMsg)														// Evaluate Window Message
	{
		case WM_SYSCOMMAND:												// Intercept System Commands
		{
			switch (wParam)												// Check System Calls
			{
				case SC_SCREENSAVE:										// Screensaver Trying To Start?
				case SC_MONITORPOWER:									// Monitor Trying To Enter Powersave?
				return 0;												// Prevent From Happening
			}
			break;														// Exit
		}
		return 0;														// Return

		case WM_CREATE:													// Window Creation
		{
			/* On create we get a programmer defined pointer to GL_Window class in lParam variable
			 * We store, so we can access it every time this procedure is called (first line of this function)*/
			CREATESTRUCT* creation = (CREATESTRUCT*)(lParam);			// Store Window Structure Pointer
			window = (GL_Window*)(creation->lpCreateParams);
			SetWindowLong (hWnd, GWL_USERDATA, (LONG)(window));
		}
		return 0;														// Return
	}

	return window->windowProc(hWnd, uMsg, wParam, lParam);					// Pass Unhandled Messages To DefWindowProc
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	/* Windows properties */
	GL_Window window("OpenGL", hInstance);
	window.title			= "ProjectRG";
	window.width			= 1280;										// Window width
	window.height			= 800;										// Window height
	window.bitsPerPixel		= 32;										// Bits per pixel
	window.fullScreen 		= true;										// Fullscreen? (Default to true)
	window.multisample		= true;										// Fulscreen AA

	/* Ask if user wants fullscreen */
	if (MessageBox (HWND_DESKTOP, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO) {
		window.fullScreen = false;									// If Not, Run In Windowed Mode
	}

	if (window.registerWindowClass(WindowProc) == false)				// Did registering a class fail?
	{
		MessageBox (HWND_DESKTOP, "Error Registering Window Class!", "Error", MB_OK | MB_ICONEXCLAMATION);
		return -1;														// Terminate Application
	}

	Loop loop(&window);
	loop.run();

	return 0;
}

